import pygame, os, datetime
from datetime import *

class Cenario:
	def __init__(self,fase):
		self.lista_de_paredes = []
		self.lista_de_bombas = []
		self.lista_de_destrutiveis = []
		self.imagem_destrutivel = pygame.image.load('imagens' + os.sep + 'fases' + os.sep + fase + os.sep + 'destrutivel.png')
		self.imagem_de_fundo = pygame.image.load('imagens' + os.sep + 'fases' + os.sep + fase + os.sep + 'background.png').convert()
		self.imagem_bomba = [pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'bomba1.png'),
							pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'bomba2.png'),
							pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'bomba3.png'),
							pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'bomba4.png')]
		self.imagem_fogo = [pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'fogo_centro.png'),
							pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'fogo_final_baixo.png'),
							pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'fogo_final_cima.png'),
							pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'fogo_final_direita.png'),
							pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'fogo_final_esquerda.png'),
							pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'fogo_horizontal.png'),
							pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomba' + os.sep + 'fogo_vertical.png')]
		if fase == 'cemiterio':
			self.mapa_de_movimentacao = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
										[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
										[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
										[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
		if fase == 'rancho':
			self.mapa_de_movimentacao = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
										[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
										[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
										[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
		if fase == 'ponte':
			self.mapa_de_movimentacao = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
										[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
										[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
										[0,1,2,2,'rmv',2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,'rmv',2,1,1,1,2,2,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,1,1,1,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,'rmv',2,1,1,1,1,1,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,1,1,1,1,1,1,2,2,2,2,2,2,2,1,0],
										[0,1,2,2,1,1,1,1,1,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,1,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0],
										[0,1,2,1,2,1,2,1,1,1,2,1,2,1,2,1,2,1,0],
										[0,1,2,2,2,2,2,1,1,1,2,2,2,2,2,2,2,1,0],
										[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],
										[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
			
		for y in range(15):
			for x in range(18):
				if self.mapa_de_movimentacao[y][x] == 1:
					self.lista_de_paredes.append(pygame.Rect(x*42,y*42,42,42))
				if self.mapa_de_movimentacao[y][x] == 'rmv':
					self.lista_de_destrutiveis.append(pygame.Rect(x*42,y*42,42,42))	

	def colisao_de_movimento(self,dx,dy,player):
		for destrutivel in self.lista_de_destrutiveis:
			if player.rect.colliderect(destrutivel):
				if dx > 0:
					player.rect.right = destrutivel.left
				if dx < 0:
					player.rect.left = destrutivel.right
				if dy > 0:
					player.rect.bottom = destrutivel.top
				if dy < 0:
					player.rect.top = destrutivel.bottom
		for parede in self.lista_de_paredes:
			if player.rect.colliderect(parede):
				if dx > 0:
					player.rect.right = parede.left
				if dx < 0:
					player.rect.left = parede.right
				if dy > 0:
					player.rect.bottom = parede.top
				if dy < 0:
					player.rect.top = parede.bottom
		for bomba in self.lista_de_bombas:
			if player.rect.colliderect(bomba.rect):
				if (bomba.rect.x-26 <= player.rect.x <= bomba.rect.x+26):
					pass
				else:
					if player.rect.x > bomba.rect.x:
						player.rect.right = player.rect.right+player.velocidade
					elif player.rect.x < bomba.rect.x:
						player.rect.right = player.rect.right-player.velocidade
				if (bomba.rect.y-26 <= player.rect.y <= bomba.rect.y+26):
					pass
				else:
					if player.rect.y > bomba.rect.y:
						player.rect.bottom = player.rect.bottom+player.velocidade
					elif player.rect.y < bomba.rect.y:
						player.rect.top = player.rect.top-player.velocidade
	def desenha_itens(self,tela,lista_de_players):
		for bomba in self.lista_de_bombas:
			bomba.processo()
			if bomba.status == 'EXPLODIU':
				for fogo in bomba.lista_de_fogo:
					self.mapa_de_movimentacao[int(fogo.y/42)][int(fogo.x/42)] = 2
				for player in lista_de_players:
					if bomba.rect in player.lista_de_bombas_do_player:
						player.lista_de_bombas_do_player.remove(bomba.rect)
						player.quantidade_de_bombas += 1
					self.mapa_de_movimentacao[int(bomba.rect.y/42)][int(bomba.rect.x/42)] = 2
					self.lista_de_bombas.remove(bomba)
			elif bomba.status == 'EXPLODINDO':
				self.__seta_explosao(bomba)
			elif bomba.status == 'ATIVADA':
				if (datetime.now() - bomba.tempo_inicial).seconds == 0:
					self.mapa_de_movimentacao[(bomba.rect.y/42)][bomba.rect.x/42] = 'B2'
				elif (datetime.now() - bomba.tempo_inicial).seconds == 1:
					self.mapa_de_movimentacao[(bomba.rect.y/42)][bomba.rect.x/42] = 'B3'
				elif (datetime.now() - bomba.tempo_inicial).seconds == 2:
					self.mapa_de_movimentacao[(bomba.rect.y/42)][bomba.rect.x/42] = 'B4'
###### colisao com outra bomba ######
			for fogo in bomba.lista_de_fogo:
				for outra_bomba in self.lista_de_bombas:
					if fogo.colliderect(outra_bomba.rect):
						outra_bomba.status = 'EXPLODINDO'
						outra_bomba.tempo_inicial = bomba.tempo_inicial
		for y in range(15):
			for x in range(18):
				if self.mapa_de_movimentacao[y][x] == 'rmv':
					tela.blit(self.imagem_destrutivel,(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'B1':
					tela.blit(self.imagem_bomba[0],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'B2':
					tela.blit(self.imagem_bomba[1],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'B3':
					tela.blit(self.imagem_bomba[2],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'B4':
					tela.blit(self.imagem_bomba[3],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'fc':
					tela.blit(self.imagem_fogo[0],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'ffb':
					tela.blit(self.imagem_fogo[1],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'ffc':
					tela.blit(self.imagem_fogo[2],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'ffd':
					tela.blit(self.imagem_fogo[3],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'ffe':
					tela.blit(self.imagem_fogo[4],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'fh':
					tela.blit(self.imagem_fogo[5],(x*42,y*42))
				if self.mapa_de_movimentacao[y][x] == 'fv':
					tela.blit(self.imagem_fogo[6],(x*42,y*42))
	def colisao_estatica(self,player):
		if player.status == 'MORTO':
			if (datetime.now() - player.hora_da_morte).seconds >= 1:
				for bomba in self.lista_de_bombas:
					self.mapa_de_movimentacao[int(bomba.rect.y/42)][int(bomba.rect.x/42)] = 2
					for fogo in bomba.lista_de_fogo:
						self.mapa_de_movimentacao[int(fogo.y/42)][int(fogo.x/42)] = 2
				self.lista_de_bombas = []
				player.vidas -= 1
				player.rect.x = player.dx
				player.rect.y = player.dy
				player.status = 'VIVO'
				player.player_imagem = pygame.image.load('imagens' + os.sep + 'sprites' + os.sep + 'bomber' + os.sep + 'baixo4.png')
		
		else:
			for bomba in self.lista_de_bombas:
				for fogo in bomba.lista_de_fogo:
					if player.rect.colliderect(fogo):
						player.status = 'MORTO'
						player.hora_da_morte = datetime.now()
	def __seta_explosao(self,bomba):
		self.mapa_de_movimentacao[(bomba.rect.y/42)][bomba.rect.x/42] = 'fc'
		if (pygame.Rect(bomba.rect.y,bomba.rect.x,42,42)) not in bomba.lista_de_fogo:
			bomba.lista_de_fogo.append(pygame.Rect(bomba.rect.x,bomba.rect.y,42,42))
		baixo, direita, cima, esquerda = True, True, True, True
		for alcance in range(1,bomba.alcance+1):
			if cima:
				if self.mapa_de_movimentacao[(bomba.rect.y/42)-alcance][bomba.rect.x/42] == 2 or self.mapa_de_movimentacao[(bomba.rect.y/42)-alcance][bomba.rect.x/42] == 'rmv' or self.mapa_de_movimentacao[(bomba.rect.y/42)-alcance][bomba.rect.x/42] == 'B1' or self.mapa_de_movimentacao[(bomba.rect.y/42)-alcance][bomba.rect.x/42] == 'B2' or self.mapa_de_movimentacao[(bomba.rect.y/42)-alcance][bomba.rect.x/42] == 'B3' or self.mapa_de_movimentacao[(bomba.rect.y/42)-alcance][bomba.rect.x/42] == 'B4':
					bomba.lista_de_fogo.append(pygame.Rect(bomba.rect.x,((bomba.rect.y/42)-alcance)*42,42,42))
					if self.mapa_de_movimentacao[(bomba.rect.y/42)-alcance][bomba.rect.x/42] == 'rmv':
						self.lista_de_destrutiveis.remove(pygame.Rect(bomba.rect.x,((bomba.rect.y/42)-alcance)*42,42,42))
						cima = False
					self.mapa_de_movimentacao[(bomba.rect.y/42)-alcance][bomba.rect.x/42] = 'fv'
					if alcance == bomba.alcance:
						self.mapa_de_movimentacao[(bomba.rect.y/42)-alcance][bomba.rect.x/42] = 'ffc'
				else:
					cima = False
			if direita:
				if self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)+alcance] == 2 or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)+alcance] == 'rmv' or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)+alcance] == 'B1' or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)+alcance] == 'B2' or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)+alcance] == 'B3' or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)+alcance] == 'B4':
					bomba.lista_de_fogo.append(pygame.Rect(((bomba.rect.x/42)+alcance)*42,bomba.rect.y,42,42))
					if self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)+alcance] == 'rmv':
						self.lista_de_destrutiveis.remove(pygame.Rect(((bomba.rect.x/42)+alcance)*42,bomba.rect.y,42,42))
						direita = False
					self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)+alcance] = 'fh'
					if alcance == bomba.alcance:
						self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)+alcance] = 'ffd'
				else:
					direita = False
			if baixo:
				if self.mapa_de_movimentacao[(bomba.rect.y/42)+alcance][bomba.rect.x/42] == 2 or self.mapa_de_movimentacao[(bomba.rect.y/42)+alcance][bomba.rect.x/42] == 'rmv' or self.mapa_de_movimentacao[(bomba.rect.y/42)+alcance][bomba.rect.x/42] == 'B1' or self.mapa_de_movimentacao[(bomba.rect.y/42)+alcance][bomba.rect.x/42] == 'B2' or self.mapa_de_movimentacao[(bomba.rect.y/42)+alcance][bomba.rect.x/42] == 'B3' or self.mapa_de_movimentacao[(bomba.rect.y/42)+alcance][bomba.rect.x/42] == 'B4':
					bomba.lista_de_fogo.append(pygame.Rect(bomba.rect.x,((bomba.rect.y/42)+alcance)*42,42,42))
					if self.mapa_de_movimentacao[(bomba.rect.y/42)+alcance][bomba.rect.x/42] == 'rmv':
						self.lista_de_destrutiveis.remove(pygame.Rect(bomba.rect.x,((bomba.rect.y/42)+alcance)*42,42,42))
						baixo = False
					self.mapa_de_movimentacao[(bomba.rect.y/42)+alcance][bomba.rect.x/42] = 'fv'
					if alcance == bomba.alcance:
						self.mapa_de_movimentacao[(bomba.rect.y/42)+alcance][bomba.rect.x/42] = 'ffb'
				else:
					baixo = False
			if esquerda:
				if self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)-alcance] == 2 or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)-alcance] == 'rmv' or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)-alcance] == 'B1' or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)-alcance] == 'B2' or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)-alcance] == 'B3' or self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)-alcance] == 'B4':
					bomba.lista_de_fogo.append(pygame.Rect(((bomba.rect.x/42)-alcance)*42,bomba.rect.y,42,42))
					if self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)-alcance] == 'rmv':
						self.lista_de_destrutiveis.remove(pygame.Rect(((bomba.rect.x/42)-alcance)*42,bomba.rect.y,42,42))
						esquerda = False
					self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)-alcance] = 'fh'
					if alcance == bomba.alcance:
						self.mapa_de_movimentacao[bomba.rect.y/42][(bomba.rect.x/42)-alcance] = 'ffe'
				else:
					esquerda = False
